package com.zhiyan.say.service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhiyan.common.util.model.Page;
import com.zhiyan.say.dao.ClassificationDao;
import com.zhiyan.say.domain.entity.Classification;

@Service
@Transactional
public class ClassificationServiceImpl implements ClassificationService {
	@Autowired 
	private ClassificationDao classificationDao;
	
	@Override
	public void add(Classification classification){
		try {
			String id = "class" + UUID.randomUUID().toString().replace("-", "");
			classification.setId(id);
//			classification.setStatus("0");
			classificationDao.add(classification);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	@Override
	public void update(Classification classification) {
		try {
			classificationDao.update(classification);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		
	}

	@Override
	public List<Classification> query(Map<String, Object> map) {
		try {
			List<Classification> list = classificationDao.query(map);
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	@Override
	public List<Classification> queryPage(Page page, Map<String, Object> map) {
		try {
			List<Classification> list = classificationDao.query(map);
			int total = list.size();
			page.setTotal(total);
			
			int pageNo = page.getPageNo();
			int pageSize = page.getPageSize();
			int start = (pageNo-1)*pageSize;
			int pageTotal = total%pageSize==0?total/pageSize:total/pageSize+1;
			page.setPageTotal(pageTotal);
			
			map.put("start", start);
			map.put("size", pageSize);
			list = classificationDao.query(map);
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	@Override
	public void reStatus(String status, List<String> idList) {
		try {
			classificationDao.reStatus(status, idList);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}


}
